package com.bestcem.xm.user.controller;

import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.component.security.annotation.AuthIgnore;
import com.bestcem.xm.user.constant.CookieConstant;
import com.bestcem.xm.user.controller.param.CreateBookingParam;
import com.bestcem.xm.user.service.WhitepaperSubscribeService;
import com.bestcem.xm.user.service.third.LuosimaoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 官网白皮书预约
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/16 11:34
 */
@Slf4j
@RestController
@RequestMapping(value = "/user/whitepapersubscribe")
public class WhitepaperSubscribeController {

    @Resource
    private WhitepaperSubscribeService whitepaperSubscribeService;
    @Resource
    private LuosimaoService luosimaoService;

    /**
     * 创建预约
     *
     * @param param 入参
     * @return com.bestcem.xm.util.XmResultJson<com.bestcem.xm.user.controller.vo.CreateUserBookingVO>
     * @author zhangfeng <feng.zhang@idiaoyan.com>
     * @date 2022/7/14 22:09
     */
    @AuthIgnore
    @PostMapping(value = "/")
    public XmResultJson<String> createBooking(@RequestBody @Valid CreateBookingParam param,
                                              @CookieValue(value = CookieConstant.CAPTCHA_COUNT, required = false, defaultValue = "0") Long captchaCount) {
        // 螺丝帽验证
        if (captchaCount > 2 && StringUtils.isNotEmpty(param.getLuotestResponse())) {
            ServiceResult<String> result = luosimaoService.checkCaptcha(param.getLuotestResponse());
            if (!result.isSuccess()) {
                return XmResultJson.fail(result.getErrorData(), result.getMsg());
            }
        }
        // 创建预约
        ServiceResult<String> serviceResult = whitepaperSubscribeService.createBooking(param);
        if (serviceResult.isSuccess()) {
            return XmResultJson.success(serviceResult.getData());
        }
        return XmResultJson.fail(serviceResult.getErrorData(), serviceResult.getMsg());
    }
}
